
//% color="#5a5dd0" iconWidth=50 iconHeight=40
namespace naive_bayes_qa_system{

    //% block="读取数据" blockType="tag"
    export function readDataTag() {}
    //% block="初始化模块" blockType="command"
    export function init(parameter: any, block: any) {

        Generator.addImport(`from naive_bayes_qa_system import NaiveBayesQASystem\nimport warnings`)
        Generator.addCode(`warnings.filterwarnings("ignore", category=UserWarning)\n`)
        Generator.addCode(`qa_system = NaiveBayesQASystem()`)

    }
    //% block="加载停用词文件路径[DIR]，将对象存储在变量[VAR]" blockType="command"
    //% DIR.shadow="string" DIR.defl="stop_words.txt"
    //% VAR.shadow="normal" VAR.defl="stop_words"
    export function readFile(parameter: any, block: any) {
        let DIR=parameter.DIR.code; 
        let VAR=parameter.VAR.code;

        Generator.addCode(`${VAR} = qa_system.init_stop_words(r${DIR})`)
        


    }
   //% block="获取停用词[VAR]数据" blockType="reporter"
   //% VAR.shadow="normal" VAR.defl="stop_words"
    export function getFilevalue(parameter: any, block: any) {
        let VAR=parameter.VAR.code; 
        Generator.addCode(`(${VAR})`)

    }

    //% block="加载数据集文件路径[DIR]后的数据存入变量[DATA]" blockType="command"
    //% DIR.shadow="string" DIR.defl="safety_dataset.csv"
    //% DATA.shadow="normal" DATA.defl="csvdata"
   export function readcsv(parameter: any, block: any) {
    let DIR=parameter.DIR.code; 

    let DATA=parameter.DATA.code;
    Generator.addCode(`${DATA} = qa_system.dump_corpus_data(r${DIR})`)

    }   
   //% block="获取数据集[VAR]中全部数据" blockType="reporter"
    //% VAR.shadow="normal" VAR.defl="csvdata"
   export function getFilecsvvalue(parameter: any, block: any) {
    let VAR=parameter.VAR.code; 
    Generator.addCode(`(${VAR})`)

    }
    //% block="获取数据集[VAR]中[ALLDATA]的数据" blockType="reporter"
    //% VAR.shadow="normal" VAR.defl="csvdata"
    //% ALLDATA.shadow="dropdown" ALLDATA.options="ALLDATA"
    export function getFilecsvkey(parameter: any, block: any) {
        let VAR=parameter.VAR.code; 
        let ALLDATA=parameter.ALLDATA.code;
        Generator.addCode(`(${VAR}[${ALLDATA}])`)
        } 
        //% block="从数据集[VAR][DATA_M]中获取键[ALL]所对应的值" blockType="reporter"
    //% VAR.shadow="normal" VAR.defl="csvdata"
    //% DATA_M.shadow="dropdown" DATA_M.options="DATA_M"
    //% ALL.shadow="string" ALL.defl="交通安全"
    export function getFilecsvkey1(parameter: any, block: any) {
        let VAR=parameter.VAR.code; 
        let ALL=parameter.ALL.code;
        let DATA_M=parameter.DATA_M.code;
        Generator.addCode(`(${VAR}[${DATA_M}][${ALL}])`)
        }     
    //% block="文本操作" blockType="tag"
    export function Tag() {}
    //% block="对[VAR]文本进行分词" blockType="reporter"
    //% VAR.shadow="string" VAR.defl="你好小安"
    export function cutstr(parameter: any, block: any) {
        let VAR=parameter.VAR.code; 
        Generator.addCode(`qa_system.key_count(${VAR})[1]`)

    }   

    //% block="对[VAR]文本去除停用词，返回数据[TYPE]类型" blockType="reporter"
    //% VAR.shadow="string" VAR.defl="你好小安，"
    //% TYPE.shadow="dropdown" TYPE.options="TYPE"
    export function removestr(parameter: any, block: any) {
        let VAR=parameter.VAR.code; 
        let TYPE=parameter.TYPE.code;
        Generator.addCode(`qa_system.remove_stop_words(${VAR},return_type = '${TYPE}')`)

    }      
    //% block="模型训练" blockType="tag"
    export function knnTag() {}


    //% block="对[DATA]文本进行TF-IDF特征提取后，存入TF-IDF特征矩阵[TFIDF]和TF-IDF特征模型[TFIDF_MODEL]" blockType="command"
    //% DATA.shadow="string" DATA.defl="你好"
    //% TFIDF.shadow="normal" TFIDF.defl="tfidf"
    //% TFIDF_MODEL.shadow="normal" TFIDF_MODEL.defl="tfidf_model"
    export function initKNNn(parameter: any, block: any) {
        let DATA=parameter.DATA.code;
        let TFIDF=parameter.TFIDF.code; 
        let TFIDF_MODEL=parameter.TFIDF_MODEL.code;
   
        Generator.addCode(`${TFIDF}, ${TFIDF_MODEL} = qa_system.get_tfidf_re(${DATA})`)
    }
    //% block="将TF-IDF特征矩阵[TFIDF]与标签列表[LAB]输入朴素贝叶斯模型进行训练，模型存入变量[MODEL]" blockType="command"
    //% TFIDF.shadow="normal" TFIDF.defl="tfidf"
    //% MODEL.shadow="normal" MODEL.defl="model"
    //% LAB.shadow="normal" LAB.defl="label"
    export function initKNNn1(parameter: any, block: any) {
        let TFIDF=parameter.TFIDF.code; 
        let MODEL=parameter.MODEL.code;
        let LAB=parameter.LAB.code;
        Generator.addCode(`${MODEL} = qa_system.train_model(${LAB}, ${TFIDF})`)
    }    
    //% block="调用TF-IDF模型[MODEL]计算文本[TEXT]的TF-IDF值" blockType="reporter"
    //% MODEL.shadow="normal" MODEL.defl="tfidf_model"
    //% TEXT.shadow="string" TEXT.defl="你好小安"
    export function askstrreplytfidf(parameter: any, block: any) {
        let TEXT=parameter.TEXT.code;
        let MODEL=parameter.MODEL.code;
        Generator.addCode(`qa_system.gettfidf(${TEXT},${MODEL})`)
    }
   //% block="模型预测" blockType="tag"
   export function askTag() {}
    //% block="加载模型[TRAIN_DATA]和TF-IDF模型[TFIDF_MODEL],设置模型可信度[NUMBER]" blockType="command"
    //% TRAIN_DATA.shadow="normal" TRAIN_DATA.defl="train_data"
    //% TFIDF_MODEL.shadow="normal" TFIDF_MODEL.defl="tfidf_model"
    //% NUMBER.shadow="number" NUMBER.defl="0.1"
    export function askstr(parameter: any, block: any) {
        let TRAIN_DATA=parameter.TRAIN_DATA.code;
        let TFIDF_MODEL=parameter.TFIDF_MODEL.code;
        let NUMBER=parameter.NUMBER.code;
        Generator.addCode(`train_data_model = ${TRAIN_DATA}
tfidf_data_model = ${TFIDF_MODEL} 
prob_assume_data = ${NUMBER}`)
    }
    //% block="利用朴素贝叶斯模型[MODEL]和TF-IDF特征模型[TFIDF_MODEL]，设置模型可信度[NUMBER]，预测[QUERY]问题的[CLASS]" blockType="reporter"
    //% QUERY.shadow="string" QUERY.defl="你好小安"
    //% CLASS.shadow="dropdown" CLASS.options="CLASS"
    //% MODEL.shadow="normal" MODEL.defl="model"
    //% TFIDF_MODEL.shadow="normal" TFIDF_MODEL.defl="tfidf_model"
    //% NUMBER.shadow="number" NUMBER.defl="0.1"
    export function askstrreply(parameter: any, block: any) {
        let QUERY=parameter.QUERY.code;
        let CLASS=parameter.CLASS.code;
        let MODEL=parameter.MODEL.code;
        let TFIDF_MODEL=parameter.TFIDF_MODEL.code;
        let NUMBER=parameter.NUMBER.code;
        Generator.addCode(`qa_system.model_predict(${MODEL},${TFIDF_MODEL}, ${QUERY},${NUMBER})[${CLASS}]`)
    }
//     //% block="加载预测问题语料库[COR]和问题列表[QUE]及答案列表[ANS],设置相似度阈值[NUMBER]" blockType="command"
//     //% COR.shadow="normal" COR.defl="corpus"
//     //% QUE.shadow="normal" QUE.defl="question"
//     //% ANS.shadow="normal" ANS.defl="answer"
//     //% NUMBER.shadow="number" NUMBER.defl="40"
//     export function askstrsim(parameter: any, block: any) {
//         let COR=parameter.COR.code;
//         let QUE=parameter.QUE.code;
//         let ANS=parameter.ANS.code;
//         let NUMBER=parameter.NUMBER.code;
//         Generator.addCode(`corpus_data = ${COR}
// question_data = ${QUE}
// answer_data = ${ANS}
// sim_assume_data = ${NUMBER}`)

//     }

    //% block="设置模糊匹配相似度阈值[NUMBER]" blockType="command"
    //% NUMBER.shadow="number" NUMBER.defl="40"
    export function askstrsim(parameter: any, block: any) {
        let NUMBER=parameter.NUMBER.code;
        Generator.addCode(`sim_assume_data = ${NUMBER}`)
    }
    //% block="导入问题语料库[COR]、答案列表[ANS],模糊匹配问题[QUERY]的答案" blockType="command"
    //% COR.shadow="normal" COR.defl="corpus"
 
    //% ANS.shadow="normal" ANS.defl="answer"

    //% QUERY.shadow="string" QUERY.defl="你好小安"
    export function askstrreplySIM(parameter: any, block: any) {
        let COR=parameter.COR.code;
     
        let ANS=parameter.ANS.code;
        let QUERY=parameter.QUERY.code;
        Generator.addCode(`text_reply, answer_value = qa_system.sim_predict_and_show_answer(${QUERY},${COR},${ANS},sim_assume_data)`)
    }
 
    //% block="模糊匹配后答案存在？" blockType="boolean"
    export function askstrreplySIM1(parameter: any, block: any) {
        Generator.addCode(`len(text_reply)>0`)
    }
    //% block="模糊匹配答案" blockType="reporter"
    export function askstrreplySIM2(parameter: any, block: any) {
        Generator.addCode(`answer_value`)
    }
}


